using BarManager.Core.ApplicationService.Interfaces;
using BarManager.Core.Entity;
using BarManager.UI.Web.HtmlHelpers;
using BarManager.UI.Web.Models;
using System.Linq;
using System.Web.Mvc;

namespace BarManager.UI.Web.Controllers
{
    [Authorize(Users = "admin")]
    public class CargoController : BaseController<CargoViewModel, Cargo, int>
    {
        public CargoController(ICargoService service)
            : base(service)
        {
        }

        public override ActionResult Index(int id = 0)
        {
            //- Creamos el modelo
            CargoViewModel model = new CargoViewModel();

            //- Buscamos el cargo
            if (id > 0) model.Entity = Service.Get(x => x.Id == id);
            
            return View(model);
        }

        public override ActionResult List(string filter, int page = 1)
        {
            CargoViewModel model = new CargoViewModel
            {
                Entities = this.Service.Find(x => filter == null || x.Descripcion.Contains(filter))
                                                    .OrderBy(x => x.Descripcion)
                                                    .Skip((page - 1) * PageSize)
                                                    .Take(PageSize),
                PagingInfo = new PagingInfo
                {
                    CurrentPage = page,
                    ItemsPerPage = PageSize,
                    TotalItems = this.Service.Find(x => filter == null || x.Descripcion.Contains(filter)).Count()
                }
            };
            return View(model);
        }

        public override ActionResult Save(CargoViewModel model)
        {
            //- Guardamos o actualizamos
            if (model.Entity.Id > 0)
                ErrorList = this.Service.Update(model.Entity);
            else
                ErrorList = this.Service.Add(model.Entity);

            return ShowError(model, ErrorList);
        }
    }
}